"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase

import datetime as dt
from django_study.models import Course, Teacher, Colleage, University

class SimpleTest(TestCase):
    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.assertEqual(1 + 1, 2)

class CourseTest(TestCase):

    def setUp(self):

        u = University(name='swjtu')
        u.save()
        c = Colleage(name='cs', university=u)
        c.save()
        t = Teacher(name='jim', age='23', intime=dt.datetime.now(), colleage=c)
        t.save()
        c = Course(name='network', teacher=t)
        c.save()

    def test_query_time(self):
        c = Course.objects.get(name='network')
        # if like this, there will be total 4 times of querying db
        self.assertEqual(c.teacher.colleage.university.name, 'swjtu')

    def test_query_time_2(self):
        """
        if like this, will only 1 time querying db
        """
        c = Course.objects.select_related().get(name='network')
        self.assertEqual(c.teacher.colleage.university.name, 'swjtu')
